package com.springclouddemo.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class JsonUtil {
    public static <T> T toObject(String json, Class<T> c) {
        if(StringUtils.isBlank(json)){
            return null;
        }
        try {
            return JSON.toJavaObject(JSONObject.parseObject(json), c);
        } catch (Exception e) {
        }
        return null;
    }
    public static <T> List<T> toList(String json, Class<T> c) {
        List<T> list = new ArrayList<>();
        if(StringUtils.isNotBlank(json)){
            try {
                List<T> data = JSON.parseArray(json, c);
                list.addAll(data);
            } catch (Exception e) {
            }
        }
        return list;
    }
}
